package com.eqdd.yiqidian.ui.gongneng.renshi.zuzhiguanli;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerDescBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.DensityUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

/**
 * @author吕志豪 .
 * @date 17-11-29  上午11:51.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class CareerDescActivity extends SlimBaseMultiEditActivity {

    private RecyclerViewCustom dataBinding;
    private int id;
    private String postName;
    private String depName;
    private ActionSheetDialog xingziDialog;
    private ActionSheetDialog xueliDialog;
    private ActionSheetDialog jingyanDialog;
    private ActionSheetDialog gangweiDialog;
    private CareerDescBean items;
    private HashMap<Integer, String> paramMaps;
    private boolean isUpdate;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "岗位描述");
    }

    @Override
    protected RecyclerView initRecyclerView() {
        isDefautDivider = false;
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 2:
                if (xingziDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 38, sheetDialog -> {
                        xingziDialog = sheetDialog;
                        xingziDialog.show();
                    }, (position, strings) -> {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(paramMaps.get(p), strings.get(position - 1));
                        update(p, map);
                    });
                } else {
                    xingziDialog.show();
                }
                break;
            case 6:
                if (xueliDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 39, sheetDialog -> {
                        xueliDialog = sheetDialog;
                        xueliDialog.show();
                    }, (position, strings) -> {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(paramMaps.get(p), strings.get(position - 1));
                        update(p, map);
                    });
                } else {
                    xueliDialog.show();
                }
                break;
            case 7:
                if (jingyanDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 40, sheetDialog -> {
                        jingyanDialog = sheetDialog;
                        jingyanDialog.show();
                    }, (position, strings) -> {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(paramMaps.get(p), strings.get(position - 1));
                        update(p, map);
                    });
                } else {
                    jingyanDialog.show();
                }
                break;
            case 11:
                if (gangweiDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 1, sheetDialog -> {
                        gangweiDialog = sheetDialog;
                        gangweiDialog.show();
                    }, (position, strings) -> {
                        HashMap<String, String> map = new HashMap<>();
                        map.put(paramMaps.get(p), strings.get(position - 1));
                        update(p, map);
                    });
                } else {
                    gangweiDialog.show();
                }
                break;
        }
    }


    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;
        id = getIntent().getIntExtra(Config.ID, 0);
        isUpdate = getIntent().getBooleanExtra(Config.IS_UPDATE, true);
        postName = getIntent().getStringExtra("postName");
        depName = getIntent().getStringExtra("depName");
        paramMaps = new HashMap<>();
        paramMaps.put(2, "jobNature");
        paramMaps.put(3, "salaryRange");
        paramMaps.put(4, "workdesc");
        paramMaps.put(5, "Bzrenshu");
        paramMaps.put(6, "educationcla");
        paramMaps.put(7, "workexpecla");
        paramMaps.put(8, "agecla");
        paramMaps.put(9, "sexcla");
        paramMaps.put(10, "pfskills");
        paramMaps.put(11, "postgenre");
    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<CareerDescBean>>post(HttpConfig.BASE_URL + HttpConfig.CAREER_DESC)
                .params("postid", id)
                .execute(new DialogCallBack<HttpResult<CareerDescBean>>(CareerDescActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<CareerDescBean>> response) {
                        HttpResult<CareerDescBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            if (isUpdate) {
                                datas.add(new SlimEditHintBean("岗位名称", items.getPostName(), 0));
                                datas.add(new SlimEditHintBean("所属部门", items.getDepName(), 1));
                                datas.add(new SlimEditEnterBean("工作性质", items.getJobNature(), 2, true, "请选择"));
                                datas.add(new SlimEditEnterBean("薪资范围", items.getSalaryRange(), 3, "请选择"));

                                datas.add(new SlimEditEnterBean("工作描述", items.getWorkdesc(), 4, "请选择"));

                                datas.add(new SlimEditEnterBean("编制人数", items.getBZrenshu() + "", 5, "请选择").addSchemes(StaticScheme.Numeric()));

                                datas.add(new SlimEditEnterBean("学历", items.getEducationcla(), 6, true, "请选择"));

                                datas.add(new SlimEditEnterBean("工作经验", items.getWorkexpecla(), 7, true, "请选择"));

                                datas.add(new SlimEditEnterBean("年龄", items.getAgecla(), 8, "请选择"));

                                datas.add(new SlimEditChooseBean("性别", items.getSexcla(), 9, "请选择", new String[]{"男", "女", "不限"}));
                                datas.add(new SlimEditEnterBean("专业技能", items.getPfskills(), 10, "请选择"));

                                datas.add(new SlimEditEnterBean("岗位类别", items.getPostgenre(), 11, true, "请选择"));

                                dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(CareerDescActivity.this, 6, 6)
                                        .setHeadHeight(DensityUtil.percentW(50))
                                        .setTitle("岗位信息", "岗位要求")
                                        .setLeftPadding(DensityUtil.percentW(30))
                                        .setRightPadding(DensityUtil.percentW(40)));
                            } else {
                                datas.add(new SlimEditHintBean("岗位名称", items.getPostName(), 0));
                                datas.add(new SlimEditHintBean("所属部门", items.getDepName(), 1));
                                datas.add(new SlimEditHintBean("工作性质", items.getJobNature(), 2));
                                datas.add(new SlimEditHintBean("薪资范围", items.getSalaryRange(), 3));

                                datas.add(new SlimEditHintBean("工作描述", items.getWorkdesc(), 4));

                                datas.add(new SlimEditHintBean("编制人数", items.getBZrenshu() + "", 5));

                                datas.add(new SlimEditHintBean("学历", items.getEducationcla(), 6));

                                datas.add(new SlimEditHintBean("工作经验", items.getWorkexpecla(), 7));

                                datas.add(new SlimEditHintBean("年龄", items.getAgecla(), 8));

                                datas.add(new SlimEditHintBean("性别", items.getSexcla(), 9));
                                datas.add(new SlimEditHintBean("专业技能", items.getPfskills(), 10));

                                datas.add(new SlimEditHintBean("岗位类别", items.getPostgenre(), 11));

                                dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(CareerDescActivity.this, 6, 6)
                                        .setHeadHeight(DensityUtil.percentW(50))
                                        .setTitle("岗位信息", "岗位要求")
                                        .setLeftPadding(DensityUtil.percentW(30))
                                        .setRightPadding(DensityUtil.percentW(40)));
                            }
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putInt(Config.ID, id);
                            bundle.putString("postName", postName);
                            bundle.putString("depName", depName);
                            startActivityForResult(AddCareerDescActivity.class, bundle, Config.ADD_CAREER_DESC);
                        }
                    }
                });
    }

    @Override
    protected boolean onChooseClickBefore(int position, String s) {
        HashMap<String, String> map = new HashMap<>();
        map.put(paramMaps.get(position), s);
        update(position, map);
        return true;
    }

    private void update(int pos, HashMap<String, String> map) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_CAREER_DESC)
                .params("userGuid", AppConstent.user.getGuid())
                .params("recordid", items.getId())
                .params("data", new Gson().toJson(map))
                .execute(new DialogCallBack<HttpResult>(CareerDescActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            String value = map.entrySet().iterator().next().getValue();
                            slimAdapterEx.getDataItem(pos).setContent(value);
                            slimAdapterEx.notifyItemChanged(pos);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            HashMap<String, String> map = new HashMap<>();
            map.put(paramMaps.get(requestCode), content);
            update(requestCode, map);
        } else if (resultCode == Config.SUCCESS && requestCode == Config.ADD_CAREER_DESC) {
            loadNetData();
        } else {
            finish();
        }
    }
}
